package org.cwnuit.course.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.cwnuit.course.dao.ClassgroupDAO;
import org.cwnuit.course.domain.Classgroup;
import org.cwnuit.course.domain.Coursebox;

/**
 * 教研室信息表Service
 * @author john
 */
public class ClassgroupService {
	private ClassgroupDAO classgroupDAO =  new ClassgroupDAO();
	/**
	 * 通过教研室ID获取教研室信息
	 * @param groupid
	 */
	public Classgroup getClassgroup(Integer groupid){
		return classgroupDAO.getClassgroup(groupid);
	}
	
	/**
	 * 获取用所有教研室信息
	 * @return
	 */
	public List<Classgroup> listClassgroup(){
		return classgroupDAO.listClassgroup();
	}
	
	/**
	 * 添加或修改教研室信息
	 * @param request
	 */
	public boolean saveClassgroup(HttpServletRequest request) {
		String groupidStr = request.getParameter("groupid")==""?"0":request.getParameter("groupid");
		int groupid = Integer.parseInt(groupidStr);
		Coursebox coursebox = (Coursebox)request.getSession().getAttribute("coursebox");
		int courseid = coursebox.getCourseid();
		String groupname = request.getParameter("groupname");
		String place = request.getParameter("place");
		String phone = request.getParameter("phone");
		String remarks = request.getParameter("remarks");
		
		Classgroup classgroup = new Classgroup();
		classgroup.setCourseid(courseid);
		classgroup.setGroupid(groupid);
		classgroup.setGroupname(groupname);
		classgroup.setPlace(place);
		classgroup.setPhone(phone);
		classgroup.setRemarks(remarks);
		try{
			if(groupid != 0){
				classgroupDAO.updateClassgroup(classgroup);
			}else{
				classgroupDAO.insertClassgroup(classgroup);
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除教研室信息
	 * @param groupid
	 */
	public boolean deleteClassgroup(Integer groupid){
		try{
			Classgroup classgroup = new Classgroup();
			classgroup.setGroupid(groupid);
			classgroupDAO.deleteClassgroup(classgroup);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}
}
